import { defineStore } from 'pinia'
import CommonService from '@/service/common'
import LocalService from '@/service/local'
import { SideMenu } from '@xuekl/cli-base/type'
import { setFullPath } from '@/utils'
import { User } from '@/entity/system/user'

const env = process.env.NODE_ENV

interface AppState {
    menus: SideMenu[]
    localMenus: SideMenu[]
    menuCollapse: boolean
    useLight: boolean
    defaultActive: string
    submenuOpen: string
    routeTags: SideMenu[]
    currentMenu: any
    menusHistory: SideMenu[]
    userInfo?: User
    screenLoading: boolean
}

const commonService = new CommonService()
const localService = new LocalService()

const useApp = defineStore('appStore', {
    state(): AppState {
        return {
            menuCollapse: false,
            useLight: false, //浅色模式
            defaultActive: '/home',
            menus: [],
            localMenus: [],
            routeTags: [],
            currentMenu: {},
            menusHistory: [],
            submenuOpen: '',
            userInfo: undefined,
            screenLoading: false
        }
    },
    actions: {
        async initMenus() {
            if (env === 'development') {
                localService.sendToken()
            }
            const data = await commonService.getMenus({})
            if (data.code === 200) {
                this.menus = setFullPath(data.data);
            }

        },
        async initUserInfo() {
            const data = await commonService.getInfo({})
            if (data.code === 200) {
                this.userInfo = data.user
            }
        }
    }
})

export default useApp